package org.tgcloud.group.web.exception;

import com.google.common.collect.Maps;
import com.sun.xml.internal.messaging.saaj.packaging.mime.MessagingException;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.context.request.NativeWebRequest;
import org.tgcloud.group.core.APPResultMessage;
import org.tgcloud.group.core.BaseLogger;

import javax.servlet.http.HttpServletRequest;
import java.io.UnsupportedEncodingException;
import java.util.Map;


@ControllerAdvice
public class DefaultExceptionHandler extends BaseLogger
{

    /*通用异常拦截*/
    @ResponseBody
	@ExceptionHandler()
	public APPResultMessage processRuntimeException(NativeWebRequest request, Exception e, HttpServletRequest req) throws UnsupportedEncodingException, MessagingException
	{
        logger.error(e.getClass().getName());
        logger.error(e.toString());
        logger.error("error URL :" + req.getServletPath());
        String exceptionName = e.getClass().getSimpleName();
        APPResultMessage message;
        switch (exceptionName)
        {
            case "MaxUploadSizeExceededException":
                message = APPResultMessage.FILE_TOO_LARGE();
                break;
            case "MissingServletRequestParameterException":
                message = APPResultMessage.MISS_PARAMETER();
                break;
            case "TypeMismatchException":
                message = APPResultMessage.TYPE_MISMATCH();
                break;
            case "NeedLoginException":
                message = APPResultMessage.NO_LOGIN();
                break;
            case "NeedPermissionAspectException":
                message = APPResultMessage.NO_ASPECT();
                break;
            case "DataNotExistsException":
                message = APPResultMessage.DATA_NOT_EXIST();
                break;
            case "HttpRequestMethodNotSupportedException":
                message = APPResultMessage.METHOD_WRONG();
                break;
            case "UnAccessException":
                message = APPResultMessage.NO_AUTH();
                break;
            case "CommonException":
                CommonException commonException = (CommonException) e;
                message = APPResultMessage.getErrorMessage(commonException.getMessage());
                break;
            default:
                Map<String,Object> params = Maps.newHashMap();
                StringBuilder sb = new StringBuilder("");
                StackTraceElement[] elements = e.getStackTrace();
                for (int i = 0; i < elements.length ; i++)
                {
                    logger.error(elements[i].toString());
                    sb.append(elements[i].toString());
                }
                params.put("content",sb.toString());
                params.put("error URL",req.getServletPath());
                params.put("request param",req.getParameterMap());
//                zdBugUp.upEx(ZDBugUp.PROJECT_WOOD,exceptionName,ZDBugUp.SERVER_T, JSON.toJSONString(params));
                message = new APPResultMessage(2000,"出现异常，及时通知后台");
        }
        return message;
	}

}
